package com.wondertek.web.controller.task;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wondertek.common.base.Result;
import com.wondertek.service.entity.business.AccessoryPo;
import com.wondertek.service.entity.business.DevicePo;
import com.wondertek.service.entity.business.RoomPo;
import com.wondertek.service.sdk.iotdh.dto.req.GetAccessoryInfoReq;
import com.wondertek.service.sdk.iotdh.dto.res.ArmingInfo;
import com.wondertek.service.sdk.iotdh.dto.res.GetAccessoryInfoRes;
import com.wondertek.service.sdk.iotdh.dto.res.PushDhMessage;
import com.wondertek.service.sdk.iotdh.service.DhDeviceService;
import com.wondertek.service.service.business.AccessoryService;
import com.wondertek.service.service.business.DeviceErrorRecordService;
import com.wondertek.service.service.business.DeviceService;
import com.wondertek.service.service.business.RoomService;
import jakarta.annotation.Resource;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @description: 配件定时任务
 * @author: qinjianhui
 * @create: 2024-08-20 14:00
 **/
@Component
public class AccessoryTask {
    @Resource
    private DhDeviceService dhDeviceService;

    @Resource
    private AccessoryService accessoryService;

    @Resource
    private DeviceService deviceService;

    @Resource
    private RoomService roomService;

    @Resource
    private DeviceErrorRecordService deviceErrorRecordService;

    //@Scheduled(cron = "0 53 16 * * ?")
    @Scheduled(cron = "*/60 * * * * ?")
    public void start() {
        System.out.println("-----------"+new Date()+"配件更新状态定时任务开始");
        //1.先去查询数据库所有网关设备
        LambdaQueryWrapper<DevicePo> devicePoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        devicePoLambdaQueryWrapper.eq(DevicePo::getIsDelete,0);
        devicePoLambdaQueryWrapper.eq(DevicePo::getDeviceTypeId,"37");
        List<DevicePo> devicePoList = deviceService.list(devicePoLambdaQueryWrapper);
        if(CollectionUtils.isNotEmpty(devicePoList)){
            for(DevicePo devicePo : devicePoList){
                //2.调用大华接口查询网关设备的配件
                try{
                    GetAccessoryInfoReq getAccessoryInfoReq = new GetAccessoryInfoReq();
                    getAccessoryInfoReq.setDeviceId(devicePo.getSnCode());
                    Result<List<GetAccessoryInfoRes>> accessoryInfo = dhDeviceService.getAccessoryInfo(getAccessoryInfoReq);
                    List<GetAccessoryInfoRes> data = accessoryInfo.getData();
                    if(CollectionUtils.isNotEmpty(data)){
                        for(GetAccessoryInfoRes getAccessoryInfoRes : data){
                            LambdaQueryWrapper<AccessoryPo> accessoryPoLambdaQueryWrapper = new LambdaQueryWrapper<>();
                            accessoryPoLambdaQueryWrapper.eq(AccessoryPo::getAccessoryCode,getAccessoryInfoRes.getId());
                            accessoryPoLambdaQueryWrapper.eq(AccessoryPo::getIsDelete,0);
                            List<AccessoryPo> list = accessoryService.list(accessoryPoLambdaQueryWrapper);
                            if(CollectionUtils.isEmpty(list)) continue;
                            AccessoryPo accessoryPo = list.get(0);
                            Integer status = getAccessoryInfoRes.getOnline()==2 ? 1:0;
                            List<ArmingInfo> armingInfoList = getAccessoryInfoRes.getArmingInfo();
                            if(CollectionUtils.isNotEmpty(armingInfoList)){
                                ArmingInfo armingInfo = armingInfoList.get(0);
                                accessoryPo.setArmingEnable(armingInfo.isEnable());
                            }
                            if(status!=accessoryPo.getStatus()){
                                accessoryPo.setStatus(status);
                                accessoryService.updateById(accessoryPo);
                                try {
                                    PushDhMessage pushDhMessage = new PushDhMessage();
                                    pushDhMessage.setType(0==status?2:4);
                                    pushDhMessage.setDeviceSn(accessoryPo.getSnCode());
                                    pushDhMessage.setName(accessoryPo.getAccessoryName());
                                    pushDhMessage.setMsgType(status == 0 ?  "offline" : "online");
                                    pushDhMessage.setAlarmTime(System.currentTimeMillis());
                                    pushDhMessage.setStatus(status);
                                    deviceErrorRecordService.rollBackErrorRecord(pushDhMessage);
                                }catch (Exception e){
                                    //发送消息失败，不做其他操作
                                }
                            }
                        }
                    }
                }catch (Exception e){
                    //将设备下的配件全部下线
                    LambdaQueryWrapper<RoomPo> roomPoLambdaQueryWrapper = new LambdaQueryWrapper<>();
                    roomPoLambdaQueryWrapper.eq(RoomPo::getDeviceId,devicePo.getId());
                    roomPoLambdaQueryWrapper.eq(RoomPo::getIsDelete,0);
                    List<RoomPo> list = roomService.list(roomPoLambdaQueryWrapper);
                    if(CollectionUtils.isNotEmpty(list)){
                        List<String> roomIds = list.stream().map(RoomPo::getId).collect(Collectors.toList());
                        LambdaQueryWrapper<AccessoryPo> accessoryPoLambdaQueryWrapper = new LambdaQueryWrapper<>();
                        accessoryPoLambdaQueryWrapper.eq(AccessoryPo::getIsDelete,0);
                        accessoryPoLambdaQueryWrapper.in(AccessoryPo::getRoomId,roomIds);
                        List<AccessoryPo> list1 = accessoryService.list(accessoryPoLambdaQueryWrapper);
                        if(CollectionUtils.isNotEmpty(list1)){
                            for(AccessoryPo accessoryPo : list1){
                                accessoryPo.setStatus(0);
                            }
                            accessoryService.updateBatchById(list1);
                        }
                    }
                }
            }
        }
        System.out.println("-----------"+new Date()+"配件更新状态定时任务结束");
    }
}

